package com.ricky;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by bgt on 2018/5/1.
 *
 * 环境变量后置处理器  加载jdbc3.properties
 * user3=myroot3
 password3=myroot3

 需要配置spring.factories中的
 org.springframework.boot.env.EnvironmentPostProcessor=com.ricky.MyEnviromentPostProcesser
 */
@Component
public class MyEnviromentPostProcesser implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
        try(InputStream input=new FileInputStream("D:/work/java/jdbc3.properties")) {
            Properties source=new Properties();
            source.load(input);
            configurableEnvironment.getPropertySources().addLast(new PropertiesPropertySource("ricky",source));

        } catch (Exception e) {
            System.out.println("获取配置异常了呢");
        }

    }
}
